package com.cdu.guoxinan.www.smartcom.service.impl;

import com.cdu.guoxinan.www.smartcom.domian.dto.ResidentQueryDTO;
import com.cdu.guoxinan.www.smartcom.domian.pojo.Resident;
import com.cdu.guoxinan.www.smartcom.domian.pojo.User;
import com.cdu.guoxinan.www.smartcom.mapper.ResidentMapper;
import com.cdu.guoxinan.www.smartcom.mapper.UserMapper;
import com.cdu.guoxinan.www.smartcom.service.ResidentService;
import com.cdu.guoxinan.www.smartcom.unit.model.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ResidentServiceImpl implements ResidentService {
    @Resource
    private ResidentMapper residentMapper;
    @Resource
    private UserMapper userMapper;
    @Override
    public PageInfo<Resident> queryPage(ResidentQueryDTO residentQueryDTO) {
        PageHelper.startPage(residentQueryDTO.getPage(),residentQueryDTO.getLimit());
        List<Resident> list = residentMapper.queryPage(residentQueryDTO);
        PageInfo<Resident> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public boolean add(Resident resident) {
        int result = residentMapper.add(resident);
        return result > 0;
    }

    @Override
    public boolean update(Resident resident) {
        int result = residentMapper.update(resident);
        return result > 0;
    }

    @Override
    public boolean delete(Integer id) {
        int result = residentMapper.delete(id);
        return result > 0;
    }

    @Override
    public Resident findByUsername(String name) {

        return residentMapper.findByUsername(name);
    }

    @Override
    public Resident findByUserId(Long userId) {
        return residentMapper.selectByUserId(userId);
    }

    @Override
    public R setUser(User user, Integer residentId) {
        userMapper.add(user);
        Integer userId = userMapper.getUserId(user.getUsername());
        int result = residentMapper.setUserId(userId, residentId);
        if (result > 0) {
            return R.ok("设置成功");
        } else {
            return R.error("设置失败");
        }
    }

    @Override
    public Resident findResidentByUserId(Integer userId) {

        return residentMapper.findByUserId(userId);
    }
}
